package com.hyts.bmsp.web.account;


import com.hyts.bmsp.service.account.UserInfoService;
import com.hyts.bmsp.service.account.UserInfoServiceImpl;
import com.hyts.bmsp.controller.WebController;
import com.hyts.bmsp.bean.ResultJsonBean;
import com.hyts.bmsp.session.WebSessionManager;
import com.jfinal.log.Log;

/**
 * @ClassName: LoginController
 * @Description: 系统登录Controller控制器
 * @author: 李博/Alex
 * @date: 2017-2-13 下午08:19:55
 */

public class LoginController extends WebController {

	/**
	 * @fieldName: log
	 * @fieldType: Log
	 * @Description: 日志输出器
	 */
	private static final Log log = Log.getLog(LoginController.class);
	
	/**
	 * @fieldName: JSP_BASE_PATH
	 * @fieldType: String
	 * @Description: jsp页面基础路径
	 */
	private static final String JSP_LOGIN_PATH = "admin/account/login";
	
	private static final String JSP_MAIN_PATH = "admin/index";
	
	private UserInfoService service = new UserInfoServiceImpl();
	
	/**
	 * @Title: index
	 * @Description: 跳转到登录页面
	 * @return: void
	 */
	public void index()
	{
		log.debug("跳转到登录页面");
		this.toJsp(JSP_LOGIN_PATH);
	}
	
	/**
	 * @Title: doLogin
	 * @Description: 执行登录操作
	 * @return: void
	 */
	 public void doLogin()
	  {
	    log.debug("执行登录操作开始");
	    ResultJsonBean result = new ResultJsonBean(true, "登录成功");

	    String userName = getParameter("userName", "");
	    String password = getParameter("password", "");
	    log.debug("用户名：" + userName + " -- 密码：" + password);

	    String flag = this.service.findPwdByUserName(userName, password);
	    result = "0".equals(flag)?new ResultJsonBean(false, "用户名或者密码错误!"):
	    		 "-1".equals(flag)?new ResultJsonBean(false, "用户名不存在!"):result;;
        //
	    if(result.isSuccess())
        {
  	      WebSessionManager.setAttribute(getRequest(), WebSessionManager.SESSION_VALUE_TOKEN_ID, flag);
  	      WebSessionManager.setAttribute(getRequest(), WebSessionManager.SESSION_KEY_TOKEN, WebSessionManager.SESSION_VALUE_TOKEN);
        }	   
	    log.debug("执行登录操作结束");
	    toJson(result);
	  }
	
	/**
	 * @Title: main
	 * @Description: 跳转到主页面
	 * @return: void
	 */
	public void main()
	{
		log.debug("跳转到系统主页面");
		this.toJsp(JSP_MAIN_PATH);
	}
	
	 public void subMain()
	  {
	    log.debug("跳转到系统主页面");
	    toJsp("admin/main/main");
	  }
	
}
